﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
using Learun.Application.Base.SystemModule;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;

namespace Learun.Application.Web.AppApi
{
    /// <summary>
    /// 图纸文件接口
    /// </summary>
    [RoutePrefix("api/DrawingCatalogueApi")]
    [HandlerApiLogin(FilterMode.Enforce)]
    [TokenAuthorize]
    public class DrawingCatalogueApiController : WebApiControllerBase
    {
        private ec_drawing_catalogueIBLL ec_drawing_catalogueIBLL = new ec_drawing_catalogueBLL();


        /// <summary>
        /// 获取图纸目录信息
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="drawingCatalogueID">图纸目录ID</param>
        /// <returns></returns>
        [HttpGet]
        [ResponseType(typeof(ec_drawing_catalogueEntity))]
        public IHttpActionResult GetEntity(string projectId, string drawingCatalogueID)
        {
            try
            {
                var entity = ec_drawing_catalogueIBLL.GetEntity(drawingCatalogueID, projectId);
                return Success(entity);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 新增图纸目录
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="strEntity">json格式的字符串，转为json对象即可</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult AddDrawingCatalogue(string projectId)
        {
            try
            {
                var httpContent = Request.Content;
                var strEntity = httpContent.ReadAsStringAsync().Result;
                ec_drawing_catalogueEntity entity = strEntity.ToObject<ec_drawing_catalogueEntity>();

                //判断同一目录是否存在目录
                var data = ec_drawing_catalogueIBLL.GetList("{UpDrawingCatalogueID:\"" + entity.UpDrawingCatalogueID + "\",\"ProjectId\":\"" + projectId + "\"}").ToList();
                data = data.FindAll(x => x.DrawingCatalogueName == entity.DrawingCatalogueName);
                if (data != null && data.Count > 0)
                {
                    entity = data[0];
                }
                else
                {
                    entity.Create();
                    ec_drawing_catalogueIBLL.InertEntity(entity, projectId);
                }

                return Success(entity);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 编辑图纸目录
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult EditDrawingCatalogue(string projectId)
        {
            try
            {
                var httpContent = Request.Content;
                var asyncContent = httpContent.ReadAsStringAsync().Result;
                ec_drawing_catalogueEntity entity = asyncContent.ToObject<ec_drawing_catalogueEntity>();

                //判断同一目录是否存在目录
                var data = ec_drawing_catalogueIBLL.GetList("{UpDrawingCatalogueID:\"" + entity.UpDrawingCatalogueID + "\",\"ProjectId\":\"" + projectId + "\"}").ToList();
                data = data.FindAll(x => x.DrawingCatalogueName == entity.DrawingCatalogueName);
                if (data != null && data.Count > 0)
                {
                    data = data.FindAll(x => x.DrawingCatalogueID != entity.DrawingCatalogueID);
                    if (data != null && data.Count > 0)
                    {
                        return Fail("该图纸目录名称已经存在！");
                    }
                }
                ec_drawing_catalogueIBLL.EditEntity(entity, projectId);
                return Success("保存成功！");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

    }
}